import os
import pyglet
from pyglons import app

import logging 
log = logging.getLogger(__name__)

def screenshot(filename=None):
    """ save a screenshot of the current screen. If filename is None 
    the filename will be "screenshotN.png" where N is the first free
    number 
    """
    if filename is None:
        filename = 'screenshot.png'
        n = 1
        while os.path.exists(filename):
            filename = 'screenshot' + str(n) + '.png'
            n += 1
    log.info("Saving to '%s'" % filename)
    pyglet.image.get_buffer_manager().get_color_buffer().save(filename)


